// ==UserScript==
// @name         Certified Dragon Slayer - Wave for Dev
// @namespace      http://qilongjitools.cn
// @description    Bootstrap script for dragon slayer.
// @exclude        http://7da.wayi.com.tw/*
// @include	       http://7da*.wayi.com.tw/*
// ==/UserScript==

// ds_stoped_window.location.host // variable for identify the ds is running or not

var basePath = "http://qilongjitools.cn/dswave_verify_tw/";
var cardId = "090312-360-BD709996-5895-17E7-90D5-CCFE09800117";
//function to generate update time stamp
var genereateTimeStamp = function(){
    return new Date().getTime();
}

addEventListener('load', function(event){
	console.log('request common file:' + basePath + 'rj.php?logic=' + encodeURIComponent('ds_common') + '&cid=' + encodeURIComponent(cardId)  + '&ts=' +  genereateTimeStamp());
    GM_xmlhttpRequest({
        method: 'GET',
        url: basePath + 'rj.php?logic=' + encodeURIComponent('ds_common') + '&cid=' + encodeURIComponent(cardId)  + '&ts=' +  genereateTimeStamp(),
        onload: function(xpr){
            eval(xpr.responseText);
            GM_xmlhttpRequest({
                method: 'GET',
                url: 'http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js',
                onload: function(xpr){
                    eval(xpr.responseText);
                    // visual style assets
                    var getDSIconPath = function(){
                        return '<img src="' + basePath + 'ds.gif" border="0" style="margin-left:2px;margin-right:2px;" />';
                    }
                    //render information bar
                    $(document.body).append('<div style="background-color:red;color:white;position:absolute;right:10px;top:20px;">' + ds_Product + ' - ' + ds_CodeName + ' ' + ds_Version + '</div>');
                    //render statistics bar
                    $(document.body).append('<div style="width:0px;height:0px;display:none;left:-10px;top:-10px;"><img src="http://img.tongji.cn.yahoo.com/878977/ystat.gif" /></div>');
                    //render message info bar      
                    $(document.body).append('<div class="ds_msg" style="background-color:red;color:white;position:absolute;right:10px;top:40px;">狀態:腳本停止 - 目前處於配置模式</div>');
                    var msg = $('div.ds_msg').get(0);
                    var log = function(txt){
                        msg.innerHTML = "狀態：" + txt;
                    }
                    //render ds news
                    $('div.col3 > h2:contains("系統公告")').before('<h2>屠龙者相关</h2><ul class="list1"><li><a href="http://www.2006wg.com/bbs/" target="_blank">屠龙者用户论坛</a></li></ul>');
                    //start/stop flag check
                    $(document.body).append('<a class="ds_linkSwitch" style="position:absolute;right:10px;top:60px;" href="javascript:{void(0)}">狀態:腳本停止 - 目前處於配置模式</a>');
                    var linkSwitch = $('a.ds_linkSwitch').get(0);
                    if (GM_getValue("ds_stoped_" + window.location.host) != null && GM_getValue("ds_stoped_" + window.location.host) != "") {
                        linkSwitch.innerHTML = "+開啟屠龍者(進入運行模式)+";
                        linkSwitch.addEventListener('click', function(){
                            GM_setValue("ds_stoped_" + window.location.host, "");
                            window.location.href = "/mindex";
                        }, false);
                    }
                    else {
                        linkSwitch.innerHTML = "-關閉屠龍者(進入配置模式)-";
                        linkSwitch.addEventListener('click', function(){
                            GM_setValue("ds_stoped_" + window.location.host, "true");
                            window.location.href = "";
                        }, false);
                    }
                    
                    
                    
                    if (GM_getValue("ds_CONFIG_" + window.location.host) != null && GM_getValue("ds_CONFIG_" + window.location.host) != "") {
                        CONFIG = extend(CONFIG, eval(GM_getValue("ds_CONFIG_" + window.location.host)));
                    }
                    // simulate the GM Variables,but safe for cross domain.
                    var GM_safeVariables = {};
                    if (GM_getValue("ds_GMSafeVariables_" + window.location.host) != null && GM_getValue("ds_GMSafeVariables_" + window.location.host) != "") {
                        GM_safeVariables = extend(GM_safeVariables, eval(GM_getValue("ds_GMSafeVariables_" + window.location.host)));
                    }
                    
                    
                    // function to load script by add <script> tag to page.and check the entry point to current page,invoke the proper function
                    // for every different page, two method need to be implemented.
                    // 1. pageIdentity_handler // for process the current page in running mode
                    // 2. pageIdentity_handler_configMode // for process the current page in config mode
                    
                    //load different logic code for current page.
                    var url = window.location.pathname.toString();
                    var identified = false;
                    var identity = null;
                    for (var i = 0; i < states.length; i++) {
                        var state = states[i];
                        if (url == state.identity) {
                            log("進入" + state.text + "處理程式");
                            identified = true;
                            identity = state.name;
                            break;
                        }
                    }
                    if (!identified) {
                        log('無法處理本頁面,請手動跳轉到首頁!');
                    }
                    else {
                        //loadScript(identity);
                        console.log('request ' + identity + ' file:' + basePath + 'rj.php?logic=' + encodeURIComponent(identity) + '&cid=' + encodeURIComponent(cardId)  + '&ts=' +  genereateTimeStamp());
                        GM_xmlhttpRequest({
                            method: 'GET',
                            url: basePath + 'rj.php?logic=' + encodeURIComponent(identity) + '&cid=' + encodeURIComponent(cardId)  + '&ts=' +  genereateTimeStamp(),
                            onload: function(xpr){
                                try {
                                    eval(xpr.responseText);
                                } 
                                catch (e) {
                                    log('無法加載處理程式,請到<a href="http://www.2006wg.com/bbs/" target="_blank">屠龍者論壇</a>关注最新消息,或回報BUG.' + e);
                                    return;
                                }
                                GM_xmlhttpRequest({
                                    method: 'GET',
                                    url: 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.5.3/jquery-ui.min.js',
                                    onload: function(xpr){
                                        eval(xpr.responseText);
                                        if (GM_getValue("ds_stoped_" + window.location.host) != null && GM_getValue("ds_stoped_" + window.location.host) != "") {
                                            funcName = identity + '_handler_configMode';
                                            log("本頁程式加載成功,目前處於配置模式,單擊以下鏈接完成配置,進入運行模式.");
                                        }
                                        else {
                                            funcName = identity + '_handler';
                                            log("本頁程式加載成功,開始執行...");
                                        }
                                        try {
                                            func = eval(funcName);
                                        } 
                                        catch (e) {
                                            log("發生致命錯誤:" + e)
                                        }
                                        if (func) {
                                            func.apply(unsafeWindow, []);
                                        }
                                        else {
                                            log("程式加載錯誤,該錯誤可能由網絡原因導致,請您稍後重試...");
                                        }
                                    }
                                });
                            }
                        });
                    }
                    //function for auto store the CONFIG by GM_setValue(), fix "Greasemonkey access violation: unsafeWindow cannot call GM_getValue."
                    window.setInterval(function(){
                        GM_setValue("ds_CONFIG_" + window.location.host, uneval(CONFIG));
                        GM_setValue("ds_GMSafeVariables_" + window.location.host, uneval(GM_safeVariables));
                    }, 500);
                    
                } // end of callback function
            }); // end of GM_xmlhttpRequest - dswave common
        } // end of callback function
    }); // end of GM_xmlhttpRequest - jquery
}, false);





